package color.koitq.picker.ad;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import color.koitq.picker.base.BaseActivity;


public abstract class AdActivity extends BaseActivity {

    public static String curVideoClass;
    protected boolean isShowDialog = false;

    public static boolean onlyOneVideo = true;
    public static long AdDelay = 60 * 60 * 1000;//广告间隔时间
    public static  long lastWatchVideo = 0;
    public static  long lastShowDialog = 0;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    protected void showVideoAd() {
        if (AdConfig.adDisable) {
            adCloseCallBack();
            return;
        }
        curVideoClass = getClass().getName();
        if (AdConfig.vacation == AdConfig.VACATION_HOLIDAY) {
            AdManager.getInstance().setActivity(this).showVideoAdOnce();
        } else {
            adCloseCallBack();
        }
    }

    private void loadDialogAd() {
        if (AdConfig.vacation == AdConfig.VACATION_WORKING) {
            return;
        }
        isShowDialog = true;
        AdManager.getInstance().setActivity(this).loadDialogAdNew();
    }

    private void showBannerAd(ViewGroup bannerView) {
        AdManager.getInstance().setActivity(this).showBannerBottom(bannerView);
    }

    protected void showSecondPageAd_TwoBanner(ViewGroup bannerView,ViewGroup bannerView2) {
        if (AdConfig.adDisable) {
            return;
        }
        if (System.currentTimeMillis() - AdActivity.lastShowDialog >= AdActivity.AdDelay) {
            AdActivity.lastShowDialog = System.currentTimeMillis();
            loadDialogAd();
            if (AdConfig.vacation != AdConfig.VACATION_WORKING) {
                showBannerAd(bannerView2);
            }
            showBannerAd(bannerView);
        }
    }

    protected void showSecondPage_OneBanner(ViewGroup bannerView) {
        if (AdConfig.adDisable) {
            return;
        }
        if (System.currentTimeMillis() - AdActivity.lastShowDialog >= AdActivity.AdDelay) {
            AdActivity.lastShowDialog = System.currentTimeMillis();
            loadDialogAd();
            showBannerAd(bannerView);
        }
    }

    protected void showFeedAd(ViewGroup viewGroup) {
        if (AdConfig.adDisable) {
            return;
        }
        AdManager.getInstance().setActivity(this).loadExpressAd(viewGroup);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void adClose(ADCloseEvent event) {
        if (getClass().getName().equals(curVideoClass)) {
            curVideoClass = null;
            adCloseCallBack();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void adDialogClose(ADDialogCloseEvent event) {
        if (isShowDialog) {
            isShowDialog = false;
        }
    }

    protected void adCloseCallBack() {
    }

}

